﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SchnelleKarten
{
    public class Deck
    {
        Texture2D cardBack;

        Random randomNumber = new Random();
        int Card = 0;

	    public List<Card> Cards = new List<Card>();

	    public Deck()
	    {
            for( int suit = 0; suit < 4; ++suit )
		    {
			    for( int card = 0; card < 13; ++card )
			    {
                    int index = (suit * 13) + card;
                    Cards.Add(new Card());
                    Cards[index].Value = card + 1;

                    switch(suit)
                    {
                        #region Suit Cases
                        case 0:
                            Cards[index].Suit = "Club";
                            break;
                        case 1:
                            Cards[index].Suit = "Diamond";
                            break;
                        case 2:
                            Cards[index].Suit = "Heart";
                            break;
                        case 3:
                            Cards[index].Suit = "Spade";
                            break;
                        #endregion
                    }
                    
			    }
		    }
            Shuffle();
	    }

        public Deck(Texture2D backing, ContentManager Content)
        {
            for (int suit = 0; suit < 4; ++suit)
            {
                for (int card = 0; card < 13; ++card)
                {
                    Cards.Add(new Card());
                    int index = (suit * 13) + card;
                    Cards[index].Value = card + 1;
                    switch (suit)
                    {
                        #region Suit Cases
                        case 0:
                            Cards[index].Suit = "Club";
                            break;
                        case 1:
                            Cards[index].Suit = "Diamond";
                            break;
                        case 2:
                            Cards[index].Suit = "Heart";
                            break;
                        case 3:
                            Cards[index].Suit = "Spade";
                            break;
                        #endregion
                    }

                    switch (Cards[index].Suit)
                    {
                        case "Club":
                            #region Hell's
                            switch (Cards[index].Value)
                            {
                                case 1:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/Ace");
                                    break;
                                case 2:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/2");
                                    break;
                                case 3:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/3");
                                    break;
                                case 4:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/4");
                                    break;
                                case 5:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/5");
                                    break;
                                case 6:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/6");
                                    break;
                                case 7:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/7");
                                    break;
                                case 8:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/8");
                                    break;
                                case 9:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/9");
                                    break;
                                case 10:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/10");
                                    break;
                                case 11:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/Jack");
                                    break;
                                case 12:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/Queen");
                                    break;
                                case 13:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Clubs/King");
                                    break;
                            }
                            #endregion
                            break;
                        case "Diamond":
                            #region Method
                            switch (Cards[index].Value)
                            {
                                case 1:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/Ace");
                                    break;
                                case 2:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/2");
                                    break;
                                case 3:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/3");
                                    break;
                                case 4:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/4");
                                    break;
                                case 5:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/5");
                                    break;
                                case 6:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/6");
                                    break;
                                case 7:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/7");
                                    break;
                                case 8:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/8");
                                    break;
                                case 9:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/9");
                                    break;
                                case 10:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/10");
                                    break;
                                case 11:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/Jack");
                                    break;
                                case 12:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/Queen");
                                    break;
                                case 13:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Diamonds/King");
                                    break;
                            }
                            #endregion
                            break;
                        case "Heart":
                            #region Of Torure
                            switch (Cards[index].Value)
                            {
                                case 1:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/Ace");
                                    break;
                                case 2:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/2");
                                    break;
                                case 3:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/3");
                                    break;
                                case 4:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/4");
                                    break;
                                case 5:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/5");
                                    break;
                                case 6:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/6");
                                    break;
                                case 7:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/7");
                                    break;
                                case 8:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/8");
                                    break;
                                case 9:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/9");
                                    break;
                                case 10:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/10");
                                    break;
                                case 11:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/Jack");
                                    break;
                                case 12:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/Queen");
                                    break;
                                case 13:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Hearts/King");
                                    break;
                            }
                            #endregion
                            break;
                        case "Spade":
                            #region I needa better way to do this.
                            switch (Cards[index].Value)
                            {
                                case 1:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/Ace");
                                    break;
                                case 2:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/2");
                                    break;
                                case 3:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/3");
                                    break;
                                case 4:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/4");
                                    break;
                                case 5:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/5");
                                    break;
                                case 6:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/6");
                                    break;
                                case 7:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/7");
                                    break;
                                case 8:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/8");
                                    break;
                                case 9:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/9");
                                    break;
                                case 10:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/10");
                                    break;
                                case 11:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/Jack");
                                    break;
                                case 12:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/Queen");
                                    break;
                                case 13:
                                    Cards[index].card = Content.Load<Texture2D>("Cards/Spades/King");
                                    break;
                            }
                            #endregion
                            break;
                    }

                }
            }
            Shuffle();
            cardBack = backing;
        }


	    public void Shuffle()
	    {
		    int random;
            Card temp;
            for (int i = 0; i < Cards.Count; i++)
		    {
                random = randomNumber.Next(Cards.Count);
                temp = Cards[i];
                Cards[i] = Cards[random];
                Cards[random] = temp;
		    }
	    }

	    public Card DealCard()
	    {
            Card retCard;

            if (Card < Cards.Count)
            {
                retCard = Cards[0];
                Cards.RemoveAt(0);
                Card++;
                return retCard;
            }
            else if (Cards.Count == 0)
            {
                return null;
            }
            else
            {
                Shuffle();
                retCard = Cards[0];
                Cards.RemoveAt(0);
                Card = 1;
                return retCard;
            }
	    }

        public Card DrawCard()
        {
            if (Card < Cards.Count)
            {
                return Cards[Card++];
            }
            else
            {
                Shuffle();
                Card = 1;
                return Cards[0];
            }
        }

        public void Draw(SpriteBatch spriteBatch, int x, int y)
        {
            spriteBatch.Draw(cardBack, new Vector2(x, y), Color.White);
        }
    }
}
